<?php

namespace app\api\service;

use app\api\lib\SuPengJun;
use app\common\model\dynamic\DynamicAudioModel;
use app\common\model\dynamic\DynamicCommentModel;
use app\common\model\dynamic\DynamicImgModel;
use app\common\model\dynamic\DynamicLikeModel;
use app\common\model\dynamic\DynamicVideoModel;
use app\common\model\order\OrderGoodsModel;
use app\common\model\user\UserModel;

class DynamicService
{

    public static function dynamic_find()
    {
        return ['id,user_id,content,circle_id,circle_name,activity_id,activity_name,activity_img,circle_avatar,province,city,country,adds,adds_name,lat,lng,create_time,browse,likes,top,order_id,type,status'];
    }

    public static function dynamic_card($data, $uid = 0)
    {
        $data->map(function ($v) use ($uid) {
            $v['is_like'] = DynamicLikeModel::where('dynamic_id', $v['id'])->where('user_id', $uid)->value('id') > 0;
            $v['user'] = UserModel::where('id', $v['user_id'])->field('name,avatar')->find();
            if ($v['type'] == 1) {
                $v['img'] = DynamicImgModel::where('dynamic_id', $v['id'])
                    ->field('url,wide,high')
                    ->find();
                $v['img_count'] = DynamicImgModel::where('dynamic_id', $v['id'])->count();
            } else if ($v['type'] == 2) {
                $v['video'] = DynamicVideoModel::where('dynamic_id', $v['id'])
                    ->field('cover,wide,high')
                    ->find();
            } else if ($v['type'] == 3) {
                $v['audio'] = DynamicAudioModel::where('dynamic_id', $v['id'])
                    ->field('cover,name,intro')
                    ->find();
            }
            $v['like_count'] = DynamicLikeModel::where('dynamic_id', $v['id'])->count();
            $v['like_count_str'] = SuPengJun::convert($v['like_count']);
            return $v;
        });
        return $data;
    }

    public static function dynamic_map($data, $uid = 0, $type = 0)
    {
        $data->map(function ($v) use ($uid, $type) {
            if ($type == 1) {
                $v['is_like'] = DynamicLikeModel::where('dynamic_id', $v['id'])->where('user_id', $uid)->value('id') > 0;
                $v['user'] = UserModel::where('id', $v['user_id'])->field('name,avatar,gender,age')->find();
            } else {
                $v['user'] = UserModel::where('id', $v['user_id'])->field('name,avatar')->find();
                $v['status_text'] = self::dynamic_status($v['status']);
                $timestamp = strtotime($v['create_time']);
                $timestampYear = date('Y', $timestamp);
                $timestampDate = date('Y-m-d', $timestamp);
                if ($timestampDate == date('Y-m-d')) {
                    $v['create_time_d'] = '今';
                } else {
                    $v['create_time_d'] = date('d', $timestamp);
                }
                if ($timestampYear == date('Y')) {
                    $v['create_time_ym'] = date('n', $timestamp) . "月";
                } else {
                    $v['create_time_ym'] = "{$timestampYear}年" . date('n', $timestamp) . "月";
                }
            }
            if ($v['type'] == 1) {
                $v['imgs'] = DynamicImgModel::where('dynamic_id', $v['id'])
                    ->field('url,wide,high')
                    ->limit(3)
                    ->select();
                $v['img_count'] = DynamicImgModel::where('dynamic_id', $v['id'])->count();
            } else if ($v['type'] == 2) {
                $v['video'] = DynamicVideoModel::where('dynamic_id', $v['id'])
                    ->field('url,cover,wide,high')
                    ->find();
            } else if ($v['type'] == 3) {
                $v['audio'] = DynamicAudioModel::where('dynamic_id', $v['id'])
                    ->field('url,cover,name,intro')
                    ->find();
            }
            if ($v['order_id']) {
                $v->goods = OrderGoodsModel::where('order_id', $v['order_id'])
                    ->field('goods_id,goods_name,product_img')
                    ->select();
            }
            $province = $v['province'];
            if (!$province) $province = $v['city'];
            if (!$province) $province = $v['country'];
            $v['province'] = SuPengJun::addsStr($province);
            $v['create_time_str'] = SuPengJun::timeStr($v['create_time']);
            $v['comment_count'] = DynamicCommentModel::where('dynamic_id', $v['id'])->count();
            if($v['comment_count'] > 9999) SuPengJun::convert($v['comment_count']);
            $v['like_count'] = DynamicLikeModel::where('dynamic_id', $v['id'])->count() + $v['likes'];
            $v['like_count_str'] = SuPengJun::convert($v['like_count']);
            if ($type == 1 && $v['comment_count']) {
                $v['comment'] = DynamicCommentModel::where('dynamic_id', $v['id'])->where('status', 1)->field('user_id,content')->find();
                if ($v['comment']) $v['comment']['user_name'] = UserModel::where('id', $v['comment']['user_id'])->value('name');
            }
            $v['browse'] = SuPengJun::convert($v['browse']);
            return $v;
        });
        return $data;
    }

    public static function comment_status($content, $status)
    {
        if ($status == 0) {
            $content = '评论内容正在审核';
        } else if ($status == 2) {
            $content = '评论内容违法违规';
        } else if ($status == 3) {
            $content = '该评论已被删除';
        }
        return $content;
    }

    public static function dynamic_status($status)
    {
        if ($status == 1) {
            $content = '审核中';
        } else if ($status == 4) {
            $content = '已驳回';
        }
        return $content ?? '';
    }
}
